﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wise.Util;
using System.Data.SqlClient;
using System.Data;

namespace Wise.Entities
{
    public class MyDbHelper : DbHelper
    {
        /// <summary>
        /// 查询列表 参数由 @P0 到@Pn 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static List<T> FindListFromTarget<T>(string sql,params object[] paras)
        {
            using (IDbConnection con = MyDbConfig.Current.CreateTargetConnection())
            {
                con.Open();
                using (IDbCommand cmd = con.CreateCommand())
                {
                    for (int i = 0; i < paras.Length; i++)
                    {
                        object it = paras[i];
                        var p = cmd.CreateParameter();
                        p.ParameterName = $"@P{i}";
                        p.Value = NullHelper.IsDateNull(it) ? DBNull.Value : it;
                        cmd.Parameters.Add(p);
                    }
                    cmd.CommandText = sql;
                    using (var reader = cmd.ExecuteReader())
                    {
                        return DbHelper.FillCollectionFromReader<T>(reader);
                    }

                }
            }
        }
    }
}
